home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 102_01.zip / PACUTIL.C < prev    next >
Text File  |  1993-06-03  |  8KB  |  396 lines

  1. #include "pacdefs.h"
  2. #include "bdscio.h"
  3.  
  4.  
  5. char kbhit()
  6.  {    char c;
  7.     if (inp(CSTAT) & CIMASK) switch(c = (inp(CDATA) & 0177))
  8.          {    case 'Q'-64:    Freeze = 0; break;
  9.             case 'S'-64:    Freeze = 1; break;
  10.             default:    InpChar = c;
  11.          }
  12.     return InpChar;
  13.  }
  14.  
  15. char getchar()
  16.  {    char c;
  17.     while (!(c = kbhit()));
  18.     InpChar = 0;    return c;
  19.  }
  20.  
  21. putchar(c)
  22.  char c;
  23.  {    if (c & 0200) switch (c &= 0177)
  24.      { case 'O':    graphics(0); revid(1); putch(' '); return;
  25.        case '+':    graphics(1); revid(0); putch('^'); return;
  26.        case 'X':    graphics(1); revid(0); putch('~'); return;
  27.        case 'M':    graphics(1); revid(0); putch('i'); return;
  28.        default:    break; }
  29.  
  30.     graphics(0); revid(0);
  31.     putch(c); }
  32.  
  33. putch(c)
  34.  char c;
  35.  {    do kbhit(); while (Freeze || !(inp(CSTAT) & COMASK));
  36.     outp(CDATA, c); }
  37.  
  38. rawio()
  39.  {
  40.     InpChar = Freeze = 0;
  41.     rev = graph = 0;
  42.     putch(033); putch('x'); putch('5');
  43.  }
  44.  
  45.  
  46. /* Initialize a board:
  47.  * Copies char array, setting 0200 ("graphics") bit.        */
  48.  
  49. in1(bd, y, cc)
  50.  char bd[BRDY][BRDX];
  51.  char *cc, y;
  52.  {    char x;
  53.     for (x=0; *cc; ) bd[y][x++] = 0200 | *cc++;
  54.     bd[y][x] = 0;
  55.  }
  56.  
  57. initbd(bd)
  58.  char bd[BRDY][BRDX];
  59.  {    char i;
  60.     in1(bd, 0, "OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO");
  61.     in1(bd, 1, "O + + + * + + + + OOO + + + + * + + + O");
  62.     in1(bd, 2, "O X OOO + OOOOO + OOO + OOOOO + OOO X O");
  63.     in1(bd, 3, "O * + + * + * + * + + * + * + * + + * O");
  64.     in1(bd, 4, "O + OOO + O + OOOOOOOOOOO + O + OOO + O");
  65.     in1(bd, 5, "O + + + * O + + + OOO + + + O * + + + O");
  66.     in1(bd, 6, "OOOOOOO + OOOOO + OOO + OOOOO + OOOOOOO");
  67.     in1(bd, 7, "      O + O + + * + + * + + O + O      ");
  68.     in1(bd, 8, "      O + O + OOO - - OOO + O + O      ");
  69.     in1(bd, 9, "OOOOOOO + O + O         O + O + OOOOOOO");
  70.     in1(bd, 10, "        * + * O         O * + *        ");
  71.     in1(bd, 11, "OOOOOOO + O + O         O + O + OOOOOOO");
  72.     in1(bd, 12, "      O + O + OOOOOOOOOOO + O + O      ");
  73.     in1(bd, 13, "      O + O * + + + + + + * O + O      ");
  74.     in1(bd, 14, "OOOOOOO + O + OOOOOOOOOOO + O + OOOOOOO");
  75.     in1(bd, 15, "O + + + * + * + + OOO + + * + * + + + O");
  76.     in1(bd, 16, "O X OOO + OOOOO + OOO + OOOOO + OOO X O");
  77.     in1(bd, 17, "O + + O * + * + * + + * + * + * O + + O");
  78.     in1(bd, 18, "OOO + O + O + OOOOOOOOOOO + O + O + OOO");
  79.     in1(bd, 19, "O + * + + O + + + OOO + + + O + + * + O");
  80.     in1(bd, 20, "O + OOOOOOOOOOO + OOO + OOOOOOOOOOO + O");
  81.     in1(bd, 21, "O + + + + + + + * + + * + + + + + + + O");
  82.     in1(bd, 22, "OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO");
  83. }
  84.  
  85.  
  86. /* Some display utilities                    */
  87.  
  88. POS(row, col)
  89.  char row, col;
  90.  {    putchar(033); putchar('Y'); putchar(row+32); putchar(col+32);
  91.  }
  92.  
  93. PLOT(a, b, c)
  94.  {    POS(a, b);
  95.     putchar(c); }
  96.  
  97. SPLOT(a, b, c)
  98.  char *c;
  99.  {    POS(a, b);
  100.     while (*c) putchar(*c++); }
  101.  
  102. /* reverse video on/off:                */
  103.  
  104. revid(on)
  105.  char on;
  106.  {    if (on)
  107.      { if (!rev) { rev=1; putch(033); putch('p'); }
  108.        return; }
  109.     if (rev) { rev=0; putch(033); putch('q'); }}
  110.  
  111. /* graphics mode on/off:                */
  112.  
  113. graphics(on)
  114.  char on;
  115.  {    if (on)
  116.      { if (!graph) { graph=1; putch(033); putch('F'); }
  117.        return; }
  118.     if (graph) { graph=0; putch(033); putch('G'); }}
  119.  
  120.  
  121.  
  122. update()
  123. {
  124.     POS(0, 52); printf("%6d", pscore);
  125.     POS(21, 57); printf("%6d", goldcnt);
  126. }
  127.  
  128. reinit()
  129. {
  130.     int locx, locy;
  131.     char tmp;
  132.  
  133.     for (locy = 0; locy < BRDY; locy++)
  134.     {
  135.         for (locx = 0; locx < BRDX; locx++)
  136.         {
  137.             tmp = initbrd[locy][locx];
  138.             brd[locy][locx] = tmp;
  139.             if ((display[locy][locx] = tmp) == CHOICE)
  140.             {
  141.                 display[locy][locx] = GOLD;
  142.             };
  143.         };
  144.     };
  145.     goldcnt = GOLDCNT;
  146.     delay -= (delay / 5);    /* hot it up */
  147. }
  148.  
  149. errgen(string)
  150. char    *string;
  151. {
  152.     SPLOT(23,45,string);
  153. }
  154.  
  155. dokill(mnum)
  156.     int mnum;
  157. {
  158.     struct pac *mptr;
  159.  
  160.     PLOT(0, 0, BEEP);
  161.     if (pacptr->danger == TRUE)
  162.     {
  163.         if (++killcnt == MAXMONSTER)
  164.         {
  165.             if (display[TRYPOS][TRXPOS] == GOLD)
  166.             {
  167.                 goldcnt--;
  168.             };
  169.             display[TRYPOS][TRXPOS] = TREASURE;
  170.             PLOT(TRYPOS, TRXPOS, TREASURE);
  171.             killcnt = 0;
  172.         };
  173.         SPLOT(5, 45, "MONSTERS KILLED: ");
  174.         POS(5, 62); printf("%1d", killcnt);
  175.         mptr = (&monst[mnum]);
  176.         mptr->ypos = MSTARTY;
  177.         mptr->xpos = MSTARTX + (2 * mnum);
  178.         mptr->stat = START;
  179.         PLOT(mptr->ypos, mptr->xpos, MONSTER);
  180.         pscore += KILLSCORE;
  181.         return(GOTONE);
  182.     };
  183.     return(TURKEY);
  184. }
  185.  
  186. /*
  187.  * clr -- issues an escape sequence to clear the display
  188. */
  189.  
  190. clr()
  191. {
  192.     putchar(033);    putchar('E');
  193. }
  194.  
  195. /*
  196.  *    display initial instructions
  197.  */
  198.  
  199. instruct()
  200. {
  201.     clr();
  202.     POS(0, 0);
  203. printf("Attention: you are in a dungeon, being chased by %c monsters!\r\n",
  204.     MONSTER);
  205. printf("There are gold coins %c scattered uniformly in the dungeon.\r\n",
  206.     GOLD);
  207. printf("One magic potion is available at each spot marked %c. Each potion will\r\n",
  208.     POTION);
  209. printf("enable you to kill monsters (%c) by touch for a limited duration. It will also\r\n", RUNNER);
  210. printf("scare them away. When you kill a monster it is regenerated, but this takes\r\n");
  211. printf("time. You can also regenerate yourself %d times. Killing all the monsters\r\n", MAXPAC);
  212. printf("results in further treasure appearing magically somewhere in the dungeon,\r\n");
  213. printf("marked by %c. There is a magic tunnel connecting the center left and\r\n", TREASURE);
  214. printf("center right parts of the dungeon. The monsters know about it! ");
  215. printf("Monsters get\r\n");
  216. printf("smarter, and the game gets faster, as your score gets higher.\r\n");
  217. printf("Use keypad arrows to move around.  Type keypad 5 to start game.\r\n");    escore(0);
  218. }
  219.  
  220. /*
  221.  * over -- game over processing
  222.  */
  223.  
  224. over()
  225. {
  226.     int i;
  227.     int line;
  228.     int scorefile;
  229.  
  230.     poll(0);    /* flush and discard input from player */
  231.     clr();
  232.     /* high score to date processing */
  233.     if (game != 0)
  234.     {
  235.         graphics(0); revid(1); rev=0;
  236.         line = 1;
  237.         POS(line++, 20);
  238.         printf("                             ");
  239.         POS(line++, 20);
  240.         printf("  G A M E   O V E R          ");
  241.         POS(line++, 20);
  242.         printf("                             ");
  243.         POS(line++, 20);
  244.         printf("  Your score: %-5u          ", pscore);
  245.         POS(line, 20);
  246.         printf("                             \033q\r\n");
  247.     };
  248.     escore(pscore);
  249.     longjmp(jbuf,1);    /* start over --LZ */
  250. /*
  251.     leave();
  252. */
  253. }
  254.  
  255. /*
  256.  * leave -- flush buffers,kill the Child, reset tty, and delete tempfile
  257.  */
  258.  
  259. leave()
  260. {
  261.     wscore();        /* Write out score file.        */
  262.     POS(23, 0);
  263.     revid(0);    graphics(0);
  264.     printf("\033G\033q\033y5");
  265.     exit(0);
  266. }
  267.  
  268. /*
  269.  * init -- does global initialization and spawns a child process to read
  270.  *      the input terminal.
  271.  */
  272.  
  273. init(argc, argv)
  274.  char **argv;
  275. {
  276.     unsigned tries;
  277.     int *p, i;
  278.     char *arg;
  279.     tries = 0;            /* previous initializations */
  280.     lastchar = DELETE;
  281.     initbd(initbrd);
  282.     initbd(brd);
  283.     initbd(display);
  284.     killcnt = 0;
  285.  
  286.     pacptr = &Pac;
  287.     pacsymb = PACMAN;
  288.  
  289.     pacstart.xpos =    PSTARTX;    pacstart.ypos = PSTARTY;
  290.     pacstart.dirn = DNULL;        pacstart.speed = SLOW;
  291.     pacstart.danger = FALSE;
  292.     pacstart.stat = 0;        /* ? */
  293.  
  294. /* Set up TTY stuff (deleted)            */
  295.  
  296.     rawio();
  297.  
  298.     /*
  299.      * New game starts here
  300.      */
  301.  
  302.     game = 0;
  303.     while ((game == 0) && (tries++ < (CLOCKRATE * 15000)))
  304.     {
  305.         poll(1);
  306.     };
  307.  
  308.     if (tries >= (CLOCKRATE * 15000)) game = 1;   /* the default game.  */
  309.  
  310.     p = 0;            /* init random number generator    */
  311.     for (i=300; i--; tries += *p++);
  312.     srand(tries);
  313.  
  314.     delay = (1000 * CLOCKRATE);
  315.  
  316.     goldcnt = GOLDCNT;
  317.     pscore = 0;
  318.     clr();
  319. }
  320.  
  321. /*
  322.  * poll -- read characters sent by input subprocess and set global flags
  323.  */
  324.  
  325. poll(sltime)
  326. {
  327.     int stop;
  328.     int charcnt;
  329.     int junk;
  330.  
  331.     stop = 0;
  332.    while (kbhit()) switch(combuf[0] = 0177 & getchar())
  333.     {
  334.     case LEFT:
  335.         pacptr->dirn = DLEFT;
  336.         break;
  337.  
  338.     case RIGHT:
  339.         pacptr->dirn = DRIGHT;
  340.         break;
  341.  
  342.     case NORTH:
  343.     case NNORTH:
  344.         pacptr->dirn = DUP;
  345.         break;
  346.  
  347.     case DOWN:
  348.     case NDOWN:
  349.         pacptr->dirn = DDOWN;
  350.         break;
  351.  
  352.     case HALT:
  353.         game = 1;
  354.         pacptr->dirn = DNULL;
  355.         break;
  356.  
  357.     case ABORT:
  358.     case DELETE:
  359.     case QUIT:
  360.         over();
  361.         break;
  362.  
  363.     case CNTLS:
  364.         stop = 1;
  365.         continue;
  366.  
  367.     case '7':
  368.     case '9':
  369.     case '1':
  370.     case '3':
  371.         game = 1;
  372.         break;
  373.  
  374.     case CONTROL_C:
  375.         leave();
  376.  
  377.     default:
  378.         continue;
  379.     }
  380. }
  381.  
  382. int getrand(range)
  383.  int range;
  384. {
  385.     return rand() % range;
  386. }
  387.  
  388.  
  389. /* saw -- a fake.                */
  390.  
  391. sleep(secs)
  392.  {    int i;
  393.     while (secs-- > 0)
  394.       for (i=(CLOCKRATE * 15000); i--; );
  395.  }
  396.